/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.csource.manage.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.punish.common.sign.dto.SignAttachmentFileCSourceSaveDto;
import com.icinfo.cloud.provider.punish.common.sign.dto.SignAttachmentFileDto;
import com.icinfo.cloud.provider.punish.csource.manage.mapper.CaseSourceMaterialRecordMapper;
import com.icinfo.cloud.provider.punish.csource.manage.model.CaseSourceDocumentRecord;
import com.icinfo.cloud.provider.punish.csource.manage.model.CaseSourceMaterialRecord;
import com.icinfo.cloud.provider.punish.csource.manage.service.ICaseSourceDocumentRecordService;
import com.icinfo.cloud.provider.punish.csource.manage.service.ICaseSourceMaterialRecordService;
import com.icinfo.cloud.provider.punish.csource.manage.service.ICaseSourcePartyInfoService;
import com.icinfo.cloud.provider.punish.csource.manage.service.ICaseSourceRegisterDetailInfoService;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 描述: 案源材料记录表 case_source_material_record 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年08月12日
 */
@Service
public class CaseSourceMaterialRecordServiceImpl extends MyBatisServiceSupport implements ICaseSourceMaterialRecordService {
    /**
     * 日志记录器
     */
    private static final Logger logger = LoggerFactory.getLogger(CaseSourceMaterialRecordServiceImpl.class);

    @Autowired
    private CaseSourceMaterialRecordMapper caseSourceMaterialRecordMapper;
    @Resource
    private ICaseSourceRegisterDetailInfoService caseSourceRegisterDetailInfoService;
    @Resource
    private ICaseSourcePartyInfoService caseSourcePartyInfoService;
    @Resource
    private ICaseSourceDocumentRecordService caseSourceDocumentRecordService;
    @Override
    public int saveSignAttachmentFile(SignAttachmentFileCSourceSaveDto saveDto) {
        logger.info("receiveSignDoc start... signReceiveDto:{}",saveDto);
        if (ObjectUtils.isEmpty(saveDto)) {
            throw new BusinessException("未指定文书案件相关信息");
        }
        String caseSourceId = saveDto.getCaseSourceId();
        if (ObjectUtils.isEmpty(caseSourceId)) {
            throw new BusinessException("未指定案源id");
        }
        String docCategoryCode = saveDto.getDocCategoryCode();
        if (ObjectUtils.isEmpty(docCategoryCode)) {
            throw new BusinessException("未指定文书目录编码");
        }
        String partyId = saveDto.getPartyId();
        String documentCatalogCode = saveDto.getDocCategoryCode();
        CaseSourceDocumentRecord caseSourceDocumentRecordUpdate = caseSourceDocumentRecordService.getCaseDocumentRecord(caseSourceId, documentCatalogCode, partyId);;
        if (caseSourceDocumentRecordUpdate == null) {
            logger.error("未匹配到文书记录-receiveSignDoc,caseId:{},partyId:{},documentCatalogCode:{}",partyId,partyId,documentCatalogCode);
            throw new BusinessException("未匹配到文书记录-receiveSignDoc");
        }
        Example example=new Example(CaseSourceMaterialRecord.class);
        example.createCriteria().andEqualTo("caseSourceId",saveDto.getCaseSourceId()).andEqualTo("partyId",partyId)
                .andEqualTo("documentCatalogCode",docCategoryCode).andEqualTo("materialTypeId","SIGN_ATTACHMENT");
        CaseSourceMaterialRecord deleteRecord=new CaseSourceMaterialRecord();
        deleteRecord.setIsDelete(Constant.STRING_1);
        caseSourceMaterialRecordMapper.updateByExampleSelective(deleteRecord,example);
        List<SignAttachmentFileDto> signAttachmentFileDtoList = saveDto.getSignAttachmentFileDtoList();
        CaseSourceMaterialRecord caseSourceMaterialRecord = new CaseSourceMaterialRecord();
        //设置当事人id
        caseSourceMaterialRecord.setPartyId(partyId);
        caseSourceMaterialRecord.setCaseSourceId(saveDto.getCaseSourceId());
        //保存签字附件
        if (ObjectUtil.isNotEmpty(signAttachmentFileDtoList) && signAttachmentFileDtoList.size() > 0) {
            for (SignAttachmentFileDto fileDto : signAttachmentFileDtoList) {
                caseSourceMaterialRecord.setMaterialTypeId("SIGN_ATTACHMENT");
                String prefix = fileDto.getAppendixNamePrefix();
                String suffix = fileDto.getAppendixNameSuffix();
                caseSourceMaterialRecord.setMaterialName(prefix + Constant.STRING_POINT + suffix);
                caseSourceMaterialRecord.setPartyId(partyId);
                caseSourceMaterialRecord.setMaterialUrl(fileDto.getDocUrl());
                caseSourceMaterialRecord.setDocumentCatalogCode(documentCatalogCode);
                caseSourceMaterialRecord.setUploadTime(LocalDateTime.now());
                caseSourceMaterialRecordMapper.insertSelective(caseSourceMaterialRecord);
            }
        }
        return 1;
    }
}
